﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class ExchangeRateAdd : AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.cpBillDate.Value = DateTime.Now.ToString("yyyy-MM-dd");
                BindDDL();
            }
            Master.ValidationGroup = "gSubmit";
        }

        private void BindDDL()
        {
            BindParameterForListControl("Currency", ddlCurrency);
            UIHelper.InsertFirstItem(ddlCurrency);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(CurrentUser.UserID))
                {
                    UIHelper.AlertMessage(this.Page, "等待时间太久，请重新填写汇率资料！");
                    return;
                }

                MdExchangerateEntity entity = new MdExchangerateEntity();
                entity.ExchangeDate = GetDateTime(cpBillDate);
                entity.Currency = GetString(ddlCurrency);
                entity.ExchangeRate = GetDecimal(txtExchangeRate.Text.Trim());
                entity.CreateDate = DateTime.Now;
                entity.CreateUserId = CurrentUser.UserID;
                entity.CreateUserName = CurrentUser.UserName;
                entity.Status = "1";

                if (MdExchangerate.InsertMdExchangerate(entity))
                {
                    base.LogAction(BaseEnum.ActionType.Add, "", true, null, new EntityBase[] { entity });
                    UIHelper.AlertMessageGoToURL(this.Page, Message.SaveSuccess, "../MasteData/ExchangeRateList.aspx");
                }
                else
                {
                    UIHelper.AlertMessage(this.Page, Message.SaveFail);
                }
            }
            catch (Exception ex)
            {
                UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }
    }
}